package cz.gillan.housecosts;

import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import java.text.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
import cz.gillan.housecosts.sql.StateDbAdapter;

public class StateAdapter extends CursorAdapter {

	
	public StateAdapter(Context context, Cursor cursor) {
		super(context, cursor);
	}
	
	private class ViewHolder {
		TextView dateTextView; 
		TextView valueTextView;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		ViewHolder viewHolder = (ViewHolder)view.getTag();
		Date date = new Date(cursor.getLong(cursor.getColumnIndexOrThrow(StateDbAdapter.KEY_DATE)) * 1000l);
		viewHolder.dateTextView.setText(DateFormat.getInstance().format(date));
		viewHolder.valueTextView.setText(cursor.getString(cursor.getColumnIndexOrThrow(StateDbAdapter.KEY_STATE)));
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
		LayoutInflater inflater = ((Activity)context).getLayoutInflater();
		View view = inflater.inflate(R.layout.state_item, null, true);
		ViewHolder viewHolder = new ViewHolder();
		viewHolder.dateTextView = (TextView) view.findViewById(R.id.stateItemDateText);
		viewHolder.valueTextView = (TextView) view.findViewById(R.id.stateItemValueText);
		view.setTag(viewHolder);
		//cursor.getString(cursor.getColumnIndexOrThrow(StateDbAdapter.KEY_DATE))
		viewHolder.dateTextView.setText("Not yet");
		viewHolder.valueTextView.setText(cursor.getString(cursor.getColumnIndexOrThrow(StateDbAdapter.KEY_STATE)));
		
		return view;
	}

}
